package com.guigu.www.erp.luolujin.controller;

import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("lljLoginSevice")
public class LljLoginService{

    @Autowired
    private UserService service;

    @RequestMapping("/tc")
    public void tc(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        req.getSession().removeAttribute("loginUser");
        req.getSession().removeAttribute("loginUserId");
        resp.sendRedirect("/examples/login.html");
    }
    @PostMapping
    public void addPro(MultipartFile multipartFile,HttpServletRequest req) throws IOException {
        //设置上传文件需要储存的路径
        String realpath="src\\main\\webapp\\assets\\img";
        //如果路径对应的文件夹不存在就创建
        File filepath = new File(realpath);
        if(!filepath.exists()){
            filepath.mkdir();
        }
        //获取文件名
        String filename = multipartFile.getOriginalFilename();
        //根据文件名称获取文件的后缀
        String suffix=filename.substring(filename.lastIndexOf("."));
        //获取随机名称
        String s = UUID.randomUUID().toString();
        //实现文件
        s+=suffix;
        File file = new File(filepath + "/" +s);
        multipartFile.transferTo(file);;

        HttpSession session=req.getSession();
        User loginUser =(User) session.getAttribute("loginUser");
        int uid=Integer.valueOf(loginUser.getUserId());
        User user=new User(uid,null,null,null,s,null,null,null);
        service.updateByPrimaryKeySelective(user);
    }
}
